﻿using System;
using cludg.Infrastructure.Core;
using cludg.Services.Environment;
using cludg.Services.Images;
using cludg.Web.Core;
using cludg.Web.Core.Utils;
using cludg.Web.Core.Bootstrappers;
using cludg.Services.Content;
using cludg.Services.Security;
using cludg.Domain.Content.Stories.Subjects;
using cludg.Domain.Users;
using cludg.Web.Core.Notifications.Application;

namespace cludg.Web.Site.Bootstrappers
{
	public class TestDataBootstrapper : Bootstrapper
	{
		protected readonly IStoryService StoryService;
		protected readonly ICommentService CommentService;
		protected readonly IAdministration Administration;
		protected readonly IImageService ImageService;
		protected readonly IMembership Membership;
		protected readonly IApplicationNotifier ApplicationNotifier;
		protected readonly ISettingService SettingService;

		public TestDataBootstrapper(
			IStoryService storyService, 
			ICommentService commentService,
			IAdministration administration,
			IImageService imageService,
			IMembership membership,
			IApplicationNotifier applicationNotifier,
			ISettingService settingService)
		{
			StoryService = storyService;
			CommentService = commentService;
			Administration = administration;
			ImageService = imageService;
			Membership = membership;
			ApplicationNotifier = applicationNotifier;
			SettingService = settingService;
		}

		public override int Priority
		{
			get
			{
				return 100;
			}
		}

		public override void Execute()
		{
			ApplicationNotifier.SessionStart += ApplicationNotifier_SessionStart;
		}

		void ApplicationNotifier_SessionStart(object sender, SessionEventArgs e)
		{
			// ADD SETTINGS
			SettingService[Const.SettingSiteName] = "CLUDG!";

			string watermarkImageId = string.Empty;
			using (var photoStream = RequestUtils.Download("http://cludg.ru/Content/Images/watermark.png"))
			{
				if (photoStream != null)
				{
					var ids = ImageService.Add(photoStream, new[] { ImageSize.Real });
					watermarkImageId = ids[0];
				}
			}
			SettingService[Const.SettingWatermarkImage] = watermarkImageId;


			// FILL TEST USERS
			var adminId = Administration.RegisterUser(new User
			{
				DisplayName = "Великий и ужасный администратор",
				Role = UserRole.Admin,
				Email = "admin@cludg.ru",
				CreationDate = DateTime.Now
			},
				"admin", "1", false);

			var authorId = Administration.RegisterUser(new User
			{
				DisplayName = "обычный автор",
				Role = UserRole.Author,
				Email = "author@cludg.ru",
				CreationDate = DateTime.Now
			},
				"author", "1", false);

			var userId = Administration.RegisterUser(new User
			{
				DisplayName = "Вася Пупкин",
				Role = UserRole.User,
				Email = "user@cludg.ru",
				CreationDate = DateTime.Now
			},
				"user", "1", false);

			var user2Id = Administration.RegisterUser(new User
			{
				DisplayName = "Jonh Smith",
				Role = UserRole.User,
				Email = "jsmith@cludg.ru",
				CreationDate = DateTime.Now
			},
				"jsmith", "1", false);

			// FILL TEST CONTENT DATA
			string fullsizeimageId = string.Empty;
			string imageId = string.Empty;
			using (var photoStream = RequestUtils.Download("http://www.tagirov.org/blog/wp-content/uploads/2008/02/google-bot.jpg"))
			{
				if (photoStream != null)
				{
					var ids = ImageService.Add(photoStream, new[] { ImageSize.Real, ImageSize.ImageSubject }, true);
					fullsizeimageId = ids[0];
					imageId = ids[1];
				}
			}

			var story = StoryService.NewStory(new ImageSubject { RealSizeImageId = fullsizeimageId, SubjectImageId = imageId },
				Membership.GetUser(adminId), Membership.GetUser(adminId), "Story Title 1", "Long long story with Title 1",
				string.Empty, "tag 1, tag 2, google");


			CommentService.NewComment(story.Id, null, Membership.GetUser(userId), "Hehey, i'm first!");
			CommentService.NewComment(story.Id, null, Membership.GetUser(user2Id), "Hehey, i'm second!");
			var comment = CommentService.NewComment(story.Id, null, Membership.GetUser(user2Id), "Hehey, i'm third!");
			comment = CommentService.NewComment(story.Id, comment.Id, Membership.GetUser(adminId), "Hehey, i'm last!");
			CommentService.NewComment(story.Id, comment.Id, Membership.GetUser(adminId), "Hehey, i'm super last!");

			//StoryService.NewStory(new VideoSubject(), adminId, "Story Title 2", "Long long story with Title 2", "Какой то странный людь");
			StoryService.NewStory(new ImageSubject(), Membership.GetUser(authorId), Membership.GetUser(authorId), "Story Title 3", "Long long story with Title 3",
				"Какой то странный людь", "google");
			StoryService.NewStory(new ImageSubject(), Membership.GetUser(adminId), Membership.GetUser(adminId), "Story Title 4", "Long long story with Title 4",
				"Какой то странный людь", "google");

			ApplicationNotifier.SessionStart -= ApplicationNotifier_SessionStart;
		}
	}
}